﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class MoveRoleRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string RoleId = (string)WebEnvironments.Request(SiteId, "RoleId");
                RoleCls ORole =
                     SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateRoleModel(SiteId,RoleId);
                AccountTypeCls[] CheckedAccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountTypeForRole(SiteId, RoleId);
                string CheckedFolderIds = "";
                for (int iIndex = 0; iIndex < CheckedAccountTypes.Length; iIndex++)
                {
                    CheckedFolderIds = CheckedFolderIds + CheckedAccountTypes[iIndex].AccountType;
                    if (iIndex < CheckedAccountTypes.Length - 1)
                    {
                        CheckedFolderIds = CheckedFolderIds + ",";
                    }
                }
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                              "<script language=javascript>\r\n" +
                              "  function ClientExecuteMove(RoleId)\r\n"+
                              "  {\r\n"+
                              "     SiteId='" + SiteId + "';\r\n" +
                              "     SelectedFolderIds=document.getElementById('txtCheckedFolderIds').value;\r\n"+
                              "     AjaxOut = Portal.Systems.Render.MoveRoleRenderBll.ServerSideMove(SiteId, RoleId, SelectedFolderIds).value;\r\n" +
                              "     if(AjaxOut.Error)\r\n"+
                              "     {\r\n"+
                              "         alert(AjaxOut.InfoMessage);\r\n"+
                              "         return;\r\n"+
                              "     }\r\n"+
                              "     window.open(AjaxOut.RetUrl,'_self');\r\n"+
                              "  }\r\n"+
                              "</script>\r\n" +


                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientExecuteMove('"+RoleId+"');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Thực hiện</div></li>\r\n" +
                              "            <li onclick=\"javascript:window.open('RoleMan.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +
                              "<input id=txtCheckedFolderIds type=hidden value=\"" + CheckedFolderIds + "\">\r\n" +
                              "<h3>Chọn nhóm cần chuyển đến</h3>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\">" + ORole.RoleName + "\r\n" +
                              "     <ul>\r\n";
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    bool Checked = false;
                    for (int iCheck = 0; iCheck < CheckedAccountTypes.Length; iCheck++)
                    {
                        if (CheckedAccountTypes[iCheck].AccountType.Equals(AccountTypes[iIndex].AccountType))
                        {
                            Checked = true;
                            break;
                        }
                    }
                    Html +=
                        "<li id=\"" + AccountTypes[iIndex].AccountType + "\" class=\"folder expanded " + (Checked ? "selected" : "") + "\"><a href=\"" + AccountTypes[iIndex].AccountType + "\" target=\"_self\">" + AccountTypes[iIndex].Description + "</a>\r\n" +
                                BuildSubTreeView(SiteId, CheckedAccountTypes, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId, 
            AccountTypeCls[] CheckedAccountTypes,
            string ParentAccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, ParentAccountType);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    bool Checked = false;
                    for (int iCheck = 0; iCheck < CheckedAccountTypes.Length; iCheck++)
                    {
                        if (CheckedAccountTypes[iCheck].AccountType.Equals(AccountTypes[iIndex].AccountType))
                        {
                            Checked = true;
                            break;
                        }
                    }
                    Html +=
                        "<li id=\"" + AccountTypes[iIndex].AccountType + "\" class=\"folder expanded " + (Checked ? "selected" : "") + "\"><a target=\"_self\" href=\"" + AccountTypes[iIndex].AccountType + "\">" + AccountTypes[iIndex].Description + "</a>\r\n" +
                                BuildSubTreeView(SiteId, CheckedAccountTypes, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideMove(
            string SiteId,
            string RoleId,
            string SelectedFolderIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] AccountTypes = SelectedFolderIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (AccountTypes.Length == 0) throw new Exception("Chưa xác định nhóm cần chuyển!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).MoveRoleToAccountType(SiteId, AccountTypes, RoleId);
                OAjaxOut.RetUrl = "RoleMan.aspx";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
